package controller;

import hibernate.Orderstab;

import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.text.SimpleDateFormat;
import java.util.List;

import org.apache.commons.io.output.ByteArrayOutputStream;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;


public class ProcessExcelAction extends ActionSupport {  
	 private InputStream excelStream = null; 
	 private String fileName="已处理订单信息";
	@Override
	public String execute() throws Exception {
		// 第一步，创建一个 webbook，对应一个Excel文件  
        HSSFWorkbook wb = new HSSFWorkbook();  
         // 第二步，在 webbook中添加一个sheet,对应Excel文件中的sheet  
        HSSFSheet sheet = wb.createSheet( "表一" );  
         // 第三步，在sheet中添加表头第0行,注意老版本 poi对Excel的行数列数有限制short  
        HSSFRow row = sheet.createRow(( int ) 0);  
         // 第四步，创建单元格，并设置值表头 设置表头居中  
        HSSFCellStyle style = wb.createCellStyle();  
        style.setAlignment(HSSFCellStyle. ALIGN_CENTER ); // 创建一个居中格式  
        HSSFCell cell = row.createCell(0);  
        cell.setCellValue( "订单ID" );  
        cell.setCellStyle(style); 
       
        cell = row.createCell(1);  
        cell.setCellValue( "预定车位数" );  
        cell.setCellStyle(style);  
        cell = row.createCell(2);  
        cell.setCellValue( "下单时间" );  
        cell.setCellStyle(style);  
        cell = row.createCell(3);  
        cell.setCellValue( "预定时间" );  
        cell.setCellStyle(style);
        cell = row.createCell(4);  
        cell.setCellValue( "完成时间" );  
        cell.setCellStyle(style);
        cell = row.createCell(5);  
        cell.setCellValue( "订单金额" );  
        cell.setCellStyle(style); 
         // 第五步，写入实体数据 实际应用中这些数据从数据库得到，  
        NewOrder neworder=new NewOrder();
        ActionContext ctx = ActionContext.getContext();
        List<Orderstab> list=neworder.treatedOrders((Integer)ctx.getSession().get("parkID"));
         for (int i = 0; i < list.size(); i++) {  
              row = sheet.createRow(( int ) i + 1);  
              Orderstab ord = (Orderstab) list.get(i);  
               // 第四步，创建单元格，并设置值  
              row.createCell(0).setCellValue(ord.getUuid());
              
              row.createCell(1).setCellValue(ord.getOrderNumber());  
              
              cell = row.createCell(2);  
              cell.setCellValue( new SimpleDateFormat("yyyy-mm-dd hh:mm:ss" ).format(ord.getPushTime()));  
              row.createCell(3).setCellValue(ord.getOrderTime()); 
              row.createCell(4).setCellValue(new SimpleDateFormat("yyyy-mm-dd hh:mm:ss" ).format(ord.getFinishTime()));
              row.createCell(5).setCellValue(ord.getPrice());
        }  
         // 第六步，将文件存到指定位置  
         try {  
              ByteArrayOutputStream bos = new ByteArrayOutputStream();  
              wb.write(bos);  
              bos.flush();  
               byte [] byteArray = bos.toByteArray();  
               excelStream = new ByteArrayInputStream(byteArray, 0, byteArray.length );  
              bos.close();  
        } catch (Exception e) {  
              e.printStackTrace();  
        }  
         return SUCCESS ;
	}
	
	 public void setFileName(String fileName) {  
         this .fileName = fileName;  
  }  
	 
   public String getFileName() {  
         try {  
               fileName = new String(fileName .getBytes(),"ISO8859-1" );  
        } catch (UnsupportedEncodingException e) {  
               // TODO Auto-generated catch block  
              e.printStackTrace();  
        }  
         return fileName ;  
  }  
   
   public void setExcelStream(InputStream downloadStream) {  
         this .excelStream = downloadStream;  
  }  
   public InputStream getExcelStream() {  
         return excelStream ;  
  }  
    
}  
